var app = getApp({});
Page({
  data: {
    phone: "",
    veri: "获取验证码",
    checked: false,
    zhucexieyi: true,
  },
  //注册协议
  check: function() {
    this.setData({
      checked: !this.data.checked
    })
  },
  gozhucexieyi: function() {
    wx.navigateTo({
      url: 'zhucexieyi',
    })
  },
  //获取手机号
  getPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取验证码
  getVeri: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (this.data.veri == "获取验证码" && this.data.phone) {
      //验证码倒计时
      that.setData({
        veri: 60
      })
      var flag = setInterval(function(veri) {
        that.setData({
          veri: that.data.veri - 1
        })
        if (that.data.veri == 0) {
          clearInterval(flag);
          that.data.veri = "获取验证码";
          that.setData({
            veri: that.data.veri
          })
        }
      }, 1000);
      wx.request({
        url: 'https://www.mamingwang.cn/api/openid/sendTemplateSMS',
        data: {
          openid: openid,
          phone: that.data.phone
        },
        method: "post",
        success(res) {
          console.log(res)
        }
      })
    } else if (this.data.veri == "获取验证码" && !this.data.phone) {
      wx.showToast({
        title: '请填写手机号',
        icon: "none",
        duration: 1500,
        mask:true
      })
    }
  },
  //注册提交
  subReg: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    //判断两次密码是否一致
    if (e.detail.value.password != e.detail.value.passwordQR) {
      wx.showToast({
        title: '两次密码不一致',
        icon: "none",
        duration: 2000,
        mask:true
      })
    } else if (!e.detail.value.phone && !e.detail.value.pwd) {
      wx.showToast({
        title: '手机号和密码不能为空',
        icon: "none",
        duration: 1500,
        mask:true
      })
    } else if (!e.detail.value.yanzhengma) {
      wx.showToast({
        title: '验证码不能为空',
        icon: "none",
        duration: 1500,
        mask:true
      })
    } else if (this.data.checked) {
      wx.request({
        url: 'https://www.mamingwang.cn/api/openid/zhu',
        data: {
          phone: e.detail.value.phone,
          pwd: e.detail.value.password,
          code: e.detail.value.yanzhengma,
          openid: openid
        },
        method: "post",
        success(res) {
          console.log(res);
          if (res.data.code == 1) {
            wx.showToast({
              title: "注册成功",
              icon: "success",
              duration: 1500,
              mask:true,
              success() {
                setTimeout(function() {
                  wx.reLaunch({
                    url: "../../shangquan/shangquan"
                  })
                }, 1500)
              }
            })
          } else {
            wx.showToast({
              title: '验证码不正确',
              icon: 'none',
              duration: 1500,
              mask:true
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '未同意注册协议',
        icon: "none",
        duration: 1500,
        mask:true
      })
    }
  },
})